package com.suse.service.vod.util;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.suse.common.util.core.StringUtil;
import com.suse.common.util.io.IoUtil;
import com.suse.service.vod.config.VodConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class VodUtil {

    /**
     * 上传视频
     * @param file 文件
     * @return 视频ID 返回值为空说明上传失败
     */
    public static String upload(MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        String originalFilename = file.getOriginalFilename();
        InputStream in = null;
        String videoId = null;
        try {
            in = file.getInputStream();
            String title = null;
            if (StringUtil.isNotEmpty(originalFilename)) {
                title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
            }
            UploadStreamRequest request = new UploadStreamRequest(VodConfig.ACCESS_KEY_ID, VodConfig.ACCESS_KEY_SECRET, title, originalFilename, in);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            videoId = response.getVideoId();
            System.out.println("videoId:" + videoId);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IoUtil.close(in);
        }
        return videoId;
    }

    /**
     * 删除视频
     * @param videoId 视频ID
     * @return 是否成功
     */
    public static boolean remove(String videoId) {
        try {
            DefaultAcsClient vodClient = getVodClient();
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            vodClient.getAcsResponse(request);
            return true;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取视频播放凭证
     * @param videoId 视频ID
     * @return 视频播放凭证 返回值为空说明上传失败
     */
    public static String getPlayAuth(String videoId) {
        DefaultAcsClient vodClient = getVodClient();
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        try {
            GetVideoPlayAuthResponse response = vodClient.getAcsResponse(request);
            return response.getPlayAuth();
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static DefaultAcsClient getVodClient(){
        DefaultProfile profile = DefaultProfile.getProfile(VodConfig.REGION, VodConfig.ACCESS_KEY_ID, VodConfig.ACCESS_KEY_SECRET);
        return new DefaultAcsClient(profile);
    }

}
